import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/LoginView.vue'
import RegisterView from '../views/RegisterView.vue'
import PatientDetailView from '../views/PatientDetailView.vue'
import PatientsView from '../views/PatientsView.vue'
import StrokeEventDetailView from '../views/StrokeEventDetailView.vue'
import StrokeEventsView from '../views/StrokeEventsView.vue'
import TreatmentsView from '../views/TreatmentsView.vue'
import MedicalInstitutionsView from '../views/MedicalInstitutionsView.vue'
import NewsAnnouncementsView from '../views/NewsAnnouncementsView.vue'
import DownloadRequestsView from '../views/DownloadRequestsView.vue'
import CategoryView from '../views/CategoryView.vue'
import AdminDashboardView from '../views/AdminDashboardView.vue'
import VideoDetailView from '../views/VideoDetailView.vue'
import SearchView from '../views/SearchView.vue'
import UserProfileView from '../views/UserProfileView.vue'
import StrokeUserProfileView from '../views/StrokeUserProfileView.vue'
import UserInfoView from '../views/UserInfoView.vue'
import PolicyDocumentsView from '../views/PolicyDocumentsView.vue'
import StatisticsView from '../views/StatisticsView.vue'

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/statistics',
    name: 'statistics',
    component: StatisticsView,
    meta: { requiresAuth: false }
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView,
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'register',
    component: RegisterView,
    meta: { requiresAuth: false }
  },
  // 患者管理路由
  {
    path: '/patients',
    name: 'patients',
    component: PatientsView,
    meta: { requiresAuth: true }
  },
  {
    path: '/patients/:id',
    name: 'patientDetail',
    component: PatientDetailView,
    props: true,
    meta: { requiresAuth: true }
  },
  // 卒中事件管理路由
  {
    path: '/stroke-events',
    name: 'strokeEvents',
    component: StrokeEventsView,
    meta: { requiresAuth: true }
  },
  {
    path: '/stroke-events/:id',
    name: 'strokeEventDetail',
    component: StrokeEventDetailView,
    props: true,
    meta: { requiresAuth: true }
  },
  // 治疗记录管理路由
  {
    path: '/treatments',
    name: 'treatments',
    component: TreatmentsView,
    meta: { requiresAuth: true }
  },
  // 医疗机构管理路由
  {
    path: '/institutions',
    name: 'medicalInstitutions',
    component: MedicalInstitutionsView,
    meta: { requiresAuth: true }
  },
  // 新闻公告管理路由
  {
    path: '/news-announcements',
    name: 'newsAnnouncements',
    component: NewsAnnouncementsView,
    meta: { requiresAuth: true }
  },
  // 下载请求管理路由
  {
    path: '/download-requests',
    name: 'downloadRequests',
    component: DownloadRequestsView,
    meta: { requiresAuth: true }
  },
  // 分类页面
  {
    path: '/category',
    name: 'category',
    component: CategoryView
  },
  // 管理员仪表盘
  {
    path: '/admin/dashboard',
    name: 'adminDashboard',
    component: AdminDashboardView,
    meta: { requiresAuth: true, requiresAdmin: true }
  },
  // 视频详情页
  {
    path: '/video/:id',
    name: 'videoDetail',
    component: VideoDetailView
  },
  // 搜索页面
  {
    path: '/search',
    name: 'search',
    component: SearchView
  },
  // 用户个人资料（旧版）
  {    path: '/profile/:id',    name: 'userProfile',    component: UserProfileView  },
  // 用户个人资料（卒中系统专用）
  {    path: '/stroke-profile/:id',    name: 'strokeUserProfile',    component: StrokeUserProfileView,    meta: { requiresAuth: true }  },
  // 用户个人信息页面
  {    path: '/user-info/:id?',    name: 'userInfo',    component: UserInfoView,    meta: { requiresAuth: true }  },
  // 政策文档管理
  {
    path: '/policy-documents',
    name: 'policyDocuments',
    component: PolicyDocumentsView
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL || '/'),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const requiresAuth = to.meta.requiresAuth === true
  const requiresAdmin = to.meta.requiresAdmin === true
  const isAuthenticated = localStorage.getItem('token')
  const userRole = localStorage.getItem('userRole')

  if (requiresAuth && !isAuthenticated) {
    // 需要认证但未登录，重定向到登录页
    next({ name: 'login' })
  } else if (requiresAdmin && userRole !== '管理员') {
    // 需要管理员权限但不是管理员
    next({ name: 'home' })
  } else {
    next()
  }
})

export default router